<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 3/16/24
 * Time: 3:34 PM
 */

namespace app\adminapi\lists\order;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\callback\OrderCallback;
use app\common\lists\ListsSearchInterface;


/**
 * Order列表
 * Class OrderLists
 * @package app\adminapi\lists
 */
class OrderCallbackLists extends BaseAdminDataLists implements ListsSearchInterface
{

    protected $channelName;

    public function __construct($channelName)
    {
        parent::__construct();

        $this->channelName = $channelName;
    }


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/02/01 10:45
     */
    public function setSearch(): array
    {
        return [];
    }


    public function lists(): array
    {

        $query = OrderCallback::with([
            'quickapp' => function ($q) {
                $q->withField('id, name');
            },
            'channel'  => function ($q) {
                $q->withoutField('create_time, update_time, delete_time');
            },
            'orders'   => function ($q) {
                $q->withField('id, money, create_time');
            },
            'account'  => function ($q) {
                $q->withField('id, advertiser_name');
            }
        ]);
        if ($this->channelName) {
            $channelName = $this->channelName;
            $query->hasWhere('channel', function ($query) use ($channelName) {
                $query->where('name', 'like', '%' . $channelName . '%');
            });
        }
        return $query->order('id', 'desc')
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();
    }


    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        return [];
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/02/01 10:45
     */
    public function count(): int
    {
        $query = OrderCallback::with('channel')->where($this->searchWhere)->where($this->queryWhere());
        if ($this->channelName) {
            $channelName = $this->channelName;
            $query->hasWhere('channel', function ($query) use ($channelName) {
                $query->where('name', 'like', '%' . $channelName . '%');
            });
        }

        return $query->count();
    }
}
